<?php
class IndexController {
    private $user_info;
    private $config = array();
    private $data   = array(
        'isFile'      => false,//是否是文件上传
        'isWater'     => false,//是否需要水印
        'isThumb'     => false,//是否需要缩略图
        'waterSwitch' => false,//是否开启水印
        'waterLogo'   => '',//水印logo图片地址
    );
    private $_http  = 'http://';
    // 错误信息
    private function _errCode($code='',$msg=''){
        if (empty($code)) return false;
        $err_code = array(
            1=>array(
                'code'          =>'1',
                'msg'           =>'success',
            ),
            2=>array(
                'code'          =>'2',
                'msg'           =>'login image server faile',
            ),
            3=>array(
                'code'          =>'3',
                'msg'           =>'上传失败',
            ),

        );
        $err_code[$code]['msg'] = !empty($msg) ? $msg :$err_code[$code]['msg'];
        return $err_code[$code] ;
    }

    //初始化
    public function __construct(){
        $account     = 'xzy';
        $password    = '599a514fcdb1996ea968110441ec680c';
        $user_info   = UC($account);
        $this->_http = is_HTTPS()? 'https://':$this->_http;

        if(empty($account)||empty($user_info)){
            return_json($this->_errCode(2));//login image server faile
        }

        $this->user_info           = $user_info;
        $this->data['isFile']      = isset($_POST['isFile'])&&!empty($_POST['isFile']) ? true:$this->data['isFile'];  
        $this->data['isWater']     = isset($_POST['isWater'])&&!empty($_POST['isWater']) ? true:$this->data['isWater'];  
        $this->data['isThumb']     = isset($_POST['isThumb'])&&!empty($_POST['isThumb']) ? true:$this->data['isThumb']; 
        $this->data['waterSwitch'] = isset($_POST['waterSwitch'])&&!empty($_POST['waterSwitch']) ? true:$this->data['waterSwitch']; 
        $this->data['waterLogo']   = isset($_POST['waterLogo'])&&!empty($_POST['waterLogo']) ? $_POST['waterLogo']:$this->data['waterLogo']; 

        $config = C();
        if(isset($this->user_info['savePath']) && !empty($this->user_info['savePath'])){
            $config['savePath'] = $this->user_info['savePath'];
        }
        //判断是否是文件，默认是图片
        if($this->data['isFile']){
            $config['saveExt'] = true;
        }
        $this->config = $config;
    }
    public function index(){
        $upload = new Upload($this->config);// 实例化上传类
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            return_json($this->_errCode(3,$upload->getError()));//上传失败
        }else{// 上传成功
            foreach ($info as $key => $image) {
                $imagePath    = $upload->rootPath.$image['savepath'].$image['savename'];//源文件路径
                $data['url']  = $this->_http.$_SERVER['HTTP_HOST'].ltrim($imagePath,'.');
                //生成缩略图片
                $data         = array_merge($data,$this->createThumb($imagePath));
                //生成水印
                $wateUrl      = $this->createWater($imagePath);
                $data['url']  = !empty($wateUrl) ? $wateUrl :$data['url'];
                $returnData[] = $data;      
            }
            return_json($this->_errCode(1),$returnData);//success
        }       
    }
    //缩略图
    public function createThumb($imagePath){
        if(empty($imagePath)){
            return array();
        }
        if(!$this->data['isThumb']){
             return array();
        }        
        if(!empty($this->user_info['thumbSize'])){
            foreach ($this->user_info['thumbSize'] as $key => $size) {
                $data[$key] = $this->_thumb($imagePath,$size['width'],$size['height']);
            }
        }
        return $data;
    }
    //水印
    public function createWater($imagePath){
        //是否需要水印
        if(!$this->data['waterSwitch'] || !$this->data['isWater']){
             return false;
        }     
        if(empty($imagePath)){
            return false;
        }
        
        return $this->_water($imagePath);
    }
    //生成水印图
    private function _water($imagePath){
        $saveUrl = $imagePath.'_water';
        $image   = new Image(); 
        $image->open($imagePath);
        $image->water($this->data['waterLogo'],9,70)->save($saveUrl);
        return $this->_http.$_SERVER['HTTP_HOST'].ltrim($saveUrl,'.');
    }
    //生成缩略图
    private function _thumb($imagePath,$width,$height){
        $saveUrl = $imagePath.'_'.$width.'x'.$height;
        $image   = new Image(); 
        $image->open($imagePath);
        $image->thumb($width, $height,Image::IMAGE_THUMB_SCALE)->save($saveUrl);
        return $this->_http.$_SERVER['HTTP_HOST'].ltrim($saveUrl,'.');
    }

}

